package com.xiaoyu.lanling.feature.im

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.util.internal.AppFunction
import android.text.TextUtils
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.utils.ListUtil
import com.xiaoyu.im.datamodel.AppMessage
import com.xiaoyu.im.datamodel.LastContentWrapper
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSystemItem
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_01_SYSTEM
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1000_TEXT
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1001_IMAGE
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1002_AUDIO
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1003_GIFT
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1004_CALL
import com.xiaoyu.lanling.util.TextViewUtil

class ConversationLastContentFunction : AppFunction<AppMessage, LastContentWrapper> {

    override operator fun invoke(message: AppMessage): LastContentWrapper {
        val wrapper = LastContentWrapper()

        val lastMessageDesc = message.payload.attrsCopy.optString("lastMessageDesc")
        wrapper.textColor = AppContext.getColor(R.color.colorSecondaryText)

        if (!TextUtils.isEmpty(lastMessageDesc)) {
            wrapper.desc = lastMessageDesc
        } else when (message.payload.messageType) {
            TYPE_01_SYSTEM -> {
                val attrs: JsonData = message.payload.attrsCopy
                val lastContent: String = attrs.optString("lastContent")
                when {
                    !TextUtils.isEmpty(lastContent) -> {
                        val lastContentColor: String = attrs.optString("lastContentColor")
                        wrapper.desc = TextViewUtil.fromHtml(lastContent).toString()
                        wrapper.textColor = ChatMessageSystemItem.parseLastContentTextColor(lastContentColor)
                    }
                    else -> {
                        val textColor: String = attrs.optString("textColor")
                        wrapper.desc = TextViewUtil.fromHtml(message.payload.text).toString()
                        wrapper.textColor = ChatMessageSystemItem.parseLastContentTextColor(textColor)
                    }
                }
            }
            TYPE_1000_TEXT -> {
                val uid = UserData.getInstance().uid
                val mentioned = message.payload.isMentionAll || ListUtil.contains(message.payload.mentionList, uid)
                if (TextUtils.equals(uid, message.senderUid) || !mentioned) {
                    wrapper.desc = message.payload.text
                } else {
                    wrapper.desc = AppContext.getString(R.string.main_message_last_content_mention_you)
                }
            }
            TYPE_1001_IMAGE -> wrapper.desc = AppContext.getString(R.string.main_message_last_content_image)
            TYPE_1002_AUDIO -> wrapper.desc = AppContext.getString(R.string.main_message_last_content_audio)
            TYPE_1003_GIFT -> {
                if (UserData.getInstance().isSelf(message.senderUid)) {
                    wrapper.desc = AppContext.getString(R.string.main_message_last_content_gift_send)
                } else {
                    wrapper.desc = AppContext.getString(R.string.main_message_last_content_gift_receive)
                    wrapper.textColor = AppContext.getColor(R.color.main_conversation_gift_receive_text_color)
                }
            }
            TYPE_1004_CALL -> wrapper.desc = AppContext.getString(R.string.main_message_last_content_call)
            else -> {
                wrapper.desc = AppContext.getString(R.string.main_message_last_content_system)
            }
        }
        return wrapper
    }
}